<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/23
 * Time: 9:10
 */

namespace Admin\Model;


use Think\Model;

class StudentModel extends Model{


    protected $depart = null;



    /**添加
     * @param $data
     * @return string
     */
    public function addStudent($data){
        $flag = $this->add($data);
        if ($flag !== false) return '000000';
        return '111111';
    }




    /**验证学号是否存在
     * @param $data
     * @return string
     */
    public function getYz($data){
        $num = $data['num'];
        $rs  = $this->field('id')->where("num = $num")->find();
        if(is_null($rs)){
            return '111111';
        }else{
            //如果查到的学生id和传入的id一样表示查到的学生就是要修改的学生
            if(isset($data['id']) && $data['id'] == $rs['id']){
                return '111111';
            }else{
                return '000000';
            }
        }

    }



    /**删除
     * @param $id
     * @return string
     */
    public function DelStudent($id){
        $flag = $this->delete($id);
        if ($flag !== false )return '000000';
        return '111111';
    }

    /**是否存在此id
     * @param $id
     * @return string
     */
    public function getDelStudent($id){
        $flag = $this->delete($id);
        if ($flag !== false )return '000000';
        return '111111';
    }


    /**修改
     * @param $data
     * @return string
     */
    public function editStudent($data){
        $flag = $this->save($data);
        if ($flag !== false )return '000000';
        return '111111';
    }

    /**获取单条记录
     * @param $data
     * @return mixed
     */
    public function getStudentById($data){
        $id = $data['id'];
        $result = $this->find($id);
        return $result;
    }

    /**
     * 专业
     * @param $id：学院id
     * @return mixed:该id下所有专业
     */
    public function getAllXyById($id){
        return  $this->field('id,name')->where("f_id = $id")->select();
    }


    /**获取学生信息，根据id获取相应的学院、专业、班级名
     * @param $where 学生筛选条件
     * @param $limit每页显示
     * @return mixed
     */
    public function getAllStudentWhere($where,$limit){
        $studentList = $this->where($where)->field('id,num,name,d_id,p_id,c_id')->limit($limit)->select();

        $arrX =  array();
        for($i = 0 ;$i<count($studentList);$i++){
            //获取学院，专业，班级的name
            $arrX = $this->getXy($studentList[$i]['c_id']);

            //学院专业班级
            $studentList[$i]['d_name'] = $arrX[2];
            $studentList[$i]['p_name'] = $arrX[1];
            $studentList[$i]['c_name'] = $arrX[0];
            //自定义数组
//            $arr[] = array(
//                'id'=> $studentList[$i]['id'],
//                'x_num' => $studentList[$i]['x_num'],
//                'name' => $studentList[$i]['name'],
//                'telphone' =>$studentList[$i]['telphone'],
//                'sex' => $studentList[$i]['sex'],
//                'x_name' =>$studentList[$i]['x_name'],
//                'zw' =>$studentList[$i]['zw'],
//                'description' =>$studentList[$i]['description'],
//                'time' =>$studentList[$i]['time']
//            );
        }

        return $studentList;
    }

    /**根据班级id获取相应的学院、专业、班级的name值
     * @param $f_id   班级id
     * @return array
     */
    public function getXy($f_id)
    {
        if(is_null($this->depart))
            $this->depart =  M('Depart');
        $res = $this->getXyRecursion($res= array(),$f_id);
        $return = array();
        foreach($res as $v){
            $return[] = $v['name'];
        }
        return $return;
    }

    /**
     * 判断 班级是否存在
     * @param $f_id 班级ID
     * #return bool
     */
    public function hasClass($f_id){
        if(is_null($this->depart))
            $this->depart =  M('Depart');
        if($this->depart->where(['id'=>$f_id])->find()){
            return true;
        }
        return false;
    }
    public function getFullClassName($f_id){
        return implode('-',array_reverse($this->getXy($f_id)));
    }
    private function getXyRecursion( &$res,$f_id){

        $arr = $this->depart->field('name,f_id')->where('id='.$f_id)->find();
        $res[] = $arr;
        if($arr['f_id'] == 0){
            return $res;
        }
        return $this->getXyRecursion($res,$arr['f_id']);
        //$arr = array_merge($arr, $Depart->field('name')->where("id=%d",$arr['f_id'])->find());
    }

    /**
     * 统计满足筛选条件的记录总数
     * @param $where
     * @return 返回满足条件记录的总数
     */
    public function getCountByWhere($where) {
        return   $this->where($where)->count();
    }


    public function getByAllId($id){
        $this->depart =  M('Depart');
        $zy = $this->depart->where('id='.$id)->field('f_id')->find();
        $xy = $this->depart->where('id='.$zy['f_id'])->field('f_id')->find();
        if(empty($zy) || empty($xy)){
            return false;
        }
        $res['x_id'] = $xy['f_id'];
        $res['z_id'] = $zy['f_id'];
        $res['c_id']= $id;
        return $res;
    }

    public function search($searchData){
        $searchData = trim($searchData);
        $condition = [
            'num'=>$searchData,
            'name'=>['LIKE',$searchData.'%'],
            '_logic'=>'OR'
        ];
        $res = $this->where($condition)->select();
        foreach($res as &$v){
            $v['xy'] = $this->getXy($v['c_id']);
        }
        unset($v);
        if(!$res){
            $this->error = "搜索不到";
            return false;
        }
        return $res;
    }


    /** 导入  添加数据
     * @param $data
     * @return int  返回值【0】成功  【1】失败 【2】重复
     */
    public function import_add($data){
        $res = $this->where('num='.$data['num'])->find();
        if(empty($res)){
            $flag = $this->add($data);
            if ($flag !== false){
                $status = 0;
            }else{
                $status = 1;
            }

        }else{
            $status = 3;
        }
        return $status;
    }



}